package bat.iog.hytool.stream;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class StreamTest {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(7, 6, 9, 3, 8, 2, 1);

        // 遍历输出符合条件的元素
        list.stream().filter(x -> x > 6).forEach(System.out::println);
        // 匹配第一个
        Optional<Integer> findFirst = list.stream().filter(x -> x > 6).findFirst();
        // 匹配任意（适用于并行流）
        Optional<Integer> findAny = list.parallelStream().filter(x -> x > 6).findAny();
        // 是否包含符合特定条件的元素
        boolean anyMatch = list.stream().anyMatch(x -> x < 6);
        System.out.println("匹配第一个值：" + findFirst.get());
        System.out.println("匹配任意一个值：" + findAny.get());
        System.out.println("是否存在大于6的值：" + anyMatch);
        stringToIntMap();
        stringToIntFlatmap();
        testPeekAndforeach();
        testForJoinStrings();
        testCollectJoinStrings();
    }

    /** 
     * 
     * 演示map的用途：一对一转换 
     */
    public static void stringToIntMap() {    
        List<String> ids = Arrays.asList("205","105","308","469","627","193","111");    
        // 使用流操作    
        List<Integer> results = ids.stream().
                map(s -> Integer.valueOf(s)).
                collect(Collectors.toList());   
        
        System.out.println(results);
    }

    public static void stringToIntFlatmap() {    
        List<String> sentences = Arrays.asList("hello world","Jia Gou Wu Dao");    
        // 使用流操作    
        List<String> results = sentences.stream()            
                .flatMap(sentence -> Arrays.stream(sentence.split(" ")))            
                .collect(Collectors.toList());    
        System.out.println(results);
    }

    public static void testPeekAndforeach() { 
        
        List<String> sentences = Arrays.asList("hello world","Jia Gou Wu Dao");    
        
        // 演示点1： 仅peek操作，最终不会执行    
        System.out.println("----before peek----");    
        sentences.stream().peek(sentence -> System.out.println(sentence));
        System.out.println("----after peek----");    
        // 演示点2： 仅foreach操作，最终会执行    
        System.out.println("----before foreach----");    
        sentences.stream().forEach(sentence -> System.out.println(sentence));    
        System.out.println("----after foreach----");    
        // 演示点3： peek操作后面增加终止操作，peek会执行    
        System.out.println("----before peek and count----");    
        sentences.stream().peek(sentence -> System.out.println(sentence)).count();    
        System.out.println("----after peek and count----");
    }

    public static void testForJoinStrings() {    
        List<String> ids = Arrays.asList("205", "10", "308", "49", "627", "193", "111", "193");    
        StringBuilder builder = new StringBuilder();    
        for (String id : ids) {       
            builder.append(id).append(',');    
        }    
        // 去掉末尾多拼接的逗号    
        builder.deleteCharAt(builder.length() - 1);    
        System.out.println("拼接后：" + builder.toString());
    }

    public static void testCollectJoinStrings() {    
        List<String> ids = Arrays.asList("205", "10", "308", "49", "627", "193", "111", "193");    
        String joinResult = ids.stream().collect(Collectors.joining(","));    
        System.out.println("拼接后：" + joinResult);
    }
}
